<script setup lang="ts">
import { shallowRef, ref } from 'vue';
import { useLargenScreen } from '@/composables/useLargenScreen';
import headerNavBar from '@/components/headerNavBar.vue';
import yearPicker from './components/yearPicker.vue';
import confirmDialog from './components/confirmDialog.vue';
import countDown from './components/countDown.vue';
// import ico2 from '@/assets/images/pledgeReward/ico_2.png';
import dayjs from 'dayjs';
import type { OnPage } from '@/composables/useList';
import { useList } from '@/composables/useList';
import { myZhiya, chexiaoshuhui } from '@/common/api/pledgeReward';
import { showConfirmDialog, showSuccessToast } from 'vant';

const yearPickerRef = shallowRef<InstanceType<typeof yearPicker>>();
const confirmDialogRef = shallowRef<InstanceType<typeof confirmDialog>>();

// 监听浏览器窗口变化
const [largenScreen] = useLargenScreen();

const server: OnPage = async (listConfig) => {
  const res = await myZhiya({ ...listConfig });
  const { list } = res.result;
  // console.log('goodList: ', list);
  return list;
};

const { list, loading, finished, error, refreshing, onLoad, onRefresh, onRefreshCurrent } = useList(server);

const yearData = ref(dayjs().format('YYYY'));

// const openTearPicker = () => {
//   yearPickerRef.value?.open();
// };

// 撤销转出
const handleRevocation = async (id: number) => {
  try {
    await showConfirmDialog({
      title: '提示',
      message: '确认撤销转出吗？',
    });
    await chexiaoshuhui({ id });
    showSuccessToast('操作成功');
    onRefreshCurrent();
  } catch {
    /** empty */
  }
};
</script>
<template>
  <div class="transferDetails scroll-hidden" :class="{ boxShadow: largenScreen }">
    <headerNavBar />
    <!-- <div class="flex px-4 py-3">
      <div class="flex items-center" @click="openTearPicker">
        <span class="text-sm font-medium mr-1">{{ yearData }}年</span>
        <van-image width="9" height="7" :src="ico2" />
      </div>
    </div> -->
    <van-pull-refresh v-model="refreshing" @refresh="onRefresh">
      <van-list
        v-model:loading="loading"
        v-model:error="error"
        :finished="finished"
        finished-text="没有更多了"
        error-text="请求失败，点击重新加载"
        @load="onLoad"
      >
        <div class="px-3 py-4">
          <!-- 转出 -->
          <div class="bg-white rounded-lg p-3 mb-2" v-for="item in list" :key="item.id">
            <div class="flex justify-between items-center">
              <span class="text-sm">转入积分</span>
              <span class="font-medium">{{ item.price }}</span>
            </div>
            <div class="text-xs text-[#AEAEAE] py-3 border-b border-[#EEEEEE]">{{ item.crt_time }}</div>
            <div class="flex justify-between items-center pt-3" v-if="item.digstatus === 0">
              <div class="text-[#999999] text-xs">{{ item.memo }}</div>
              <van-button
                class="!w-[80px] !h-[32px]"
                type="primary"
                color="#EC2D69"
                round
                @click="confirmDialogRef?.open(item.id)"
              >
                {{ item.status }}
              </van-button>
            </div>
            <div class="flex justify-between items-center pt-3" v-else-if="item.digstatus === 1">
              <div class="text-[#EC2D69] text-xs flex items-center">
                转出倒计时：<countDown :time="item.daojishi || 0" />
              </div>
              <van-button class="!w-[80px] !h-[32px] !text-[#999999]" round @click="handleRevocation(item.id)">
                撤销
              </van-button>
            </div>
            <div class="flex justify-between items-center pt-3 text-[#999999] text-xs" v-else-if="item.digstatus === 3">
              <div>{{ item.memo }}</div>
              <div>{{ item.status }}</div>
            </div>
          </div>
        </div>
        <template v-if="finished && !list.length">
          <van-empty />
        </template>
      </van-list>
    </van-pull-refresh>

    <!-- 年份选择框 -->
    <yearPicker v-model="yearData" ref="yearPickerRef" />
    <!-- 转出确认框 -->
    <confirmDialog ref="confirmDialogRef" @onRefresh="onRefreshCurrent" />
    <!-- 底部安全区 -->
    <div class="van-safe-area-bottom"></div>
  </div>
</template>
<style lang="less" scoped>
.transferDetails {
  overflow-y: auto;
  background-color: #f6f6f6;
  height: 100vh;
  max-width: 450px;
  padding-bottom: 20px;
}
.transferDetails.boxShadow {
  margin: 3vh auto;
  box-shadow: 0 8px 32px 0 rgba(31, 38, 135, 0.37);
  backdrop-filter: blur(4px);
  -webkit-backdrop-filter: blur(4px);
  border-radius: 10px;
  border: 1px solid rgba(255, 255, 255, 0.18);
}

.scroll-hidden {
  scrollbar-width: thin;
  scrollbar-color: transparent transparent;
}

.scroll-hidden::-webkit-scrollbar {
  width: 0px;
}

.scroll-hidden::-webkit-scrollbar-track {
  background-color: transparent;
}

.scroll-hidden::-webkit-scrollbar-thumb {
  background-color: transparent;
}
</style>
